{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RoBERTa-MPU for English sentence classification\n",
    "\n",
    "## This is a demo for the ChatGPT detector in [MPU](http://arxiv.org/abs/2305.18149).\n",
    "\n",
    "### download pretrained weights and install dependencies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# install dependencies\n",
    "!pip install git+https://github.com/mindspore-lab/mindone.git\n",
    "!pip install git+https://github.com/mindspore-lab/mindnlp.git\n",
    "\n",
    "# download some pretrained weights\n",
    "!wget https://download.mindspore.cn/toolkits/mindone/detect_chatgpt/roberta_18plus.ckpt\n",
    "!wget https://download.mindspore.cn/toolkits/mindone/detect_chatgpt/tokenizer.json\n",
    "!mv roberta_18plus.ckpt examples/detect_chatgpt/\n",
    "!mv tokenizer.json examples/detect_chatgpt/"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### use our pretrained classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mindone.pipelines.text_classifiers import BertMPUSequenceClassificationPipeline\n",
    "\n",
    "pipeline = BertMPUSequenceClassificationPipeline(\n",
    "    model_name='roberta_base',\n",
    "    config_path='examples/detect_chatgpt/config.json',\n",
    "    tokenizer_path='examples/detect_chatgpt/tokenizer.json',\n",
    ")\n",
    "pipeline.load_from_pretrained('roberta_18plus.ckpt')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### get some test inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_sentences = [\n",
    "    \"They are not colored . Just as white paint is usually made from minerals found in clay . The crystals in white paint reflects all light equally making it appear white . Just liek snow . Primarily the eye color is based on the density and distribution of melanin in the eye . It just looks a certain color when light illuminates the eye . It reflects light unqually .\"\n",
    "    \"Piracy and copyright law can be contentious issues on the internet because they involve complex questions about how to balance the rights of creators and the interests of consumers. Some people argue that artists should have the right to control how their works are distributed and to charge what they feel is appropriate, while others believe that the free exchange of information is important and that artists should not be able to control how their works are used. It's important to remember that copyright law exists to protect the rights of creators and to encourage the creation of new works by ensuring that artists can earn a fair income from their creations. When someone pirates (unauthorized copying) or uses a copyrighted work without permission, they are taking something that belongs to someone else and using it for their own benefit, without paying the person who created it. This can be seen as unfair to the creator and can discourage them from creating new works in the future. At the same time, it's also important to recognize that not everyone has the same access to information and that copyright laws can sometimes make it difficult or impossible for people to access the works they want to use. This is why it's important to have a balance between protecting the rights of creators and ensuring that everyone has access to the information and works they need.\"\n",
    "]\n",
    "test_labels = [0, 1] # 0 is human, 1 is gpt\n",
    "label_to_meaning = ['human written', 'machine generated']"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### run detection!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i, text in enumerate(test_sentences):\n",
    "    probs = pipeline.predict(text)\n",
    "    print(f'text {i} result:', probs)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bert-MPU for Chinese sentence classification\n",
    "\n",
    "## This is a demo for the ChatGPT detector in [MPU](http://arxiv.org/abs/2305.18149).\n",
    "\n",
    "### download pretrained weights and install dependencies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# install dependencies\n",
    "!pip install git+https://github.com/mindspore-lab/mindone.git\n",
    "!pip install git+https://github.com/mindspore-lab/mindnlp.git\n",
    "\n",
    "# download some pretrained weights\n",
    "!wget https://download.mindspore.cn/toolkits/mindone/detect_chatgpt/bert_18plus.ckpt\n",
    "!wget https://download.mindspore.cn/toolkits/mindone/detect_chatgpt/tokenizer_zh.json\n",
    "!mv bert_18plus.ckpt examples/detect_chatgpt/\n",
    "!mv tokenizer_zh.json examples/detect_chatgpt/"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### use our pretrained classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipeline = BertMPUSequenceClassificationPipeline(\n",
    "    model_name='bert_base',\n",
    "    config_path='examples/detect_chatgpt/config_zh.json',\n",
    "    tokenizer_path='examples/detect_chatgpt/tokenizer_zh.json',\n",
    ")\n",
    "pipeline.load_from_pretrained('bert_18plus.ckpt')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### some tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_sentences = [\n",
    "    \"程序流程图又称程序框图，是用统一规定的标准符号描述程序运行具体步骤的图形表示。\"\n",
    "]\n",
    "test_labels = [0, 1] # 0 is human, 1 is gpt\n",
    "label_to_meaning = ['人类', '机器']"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i, text in enumerate(test_sentences):\n",
    "    probs = pipeline.predict(text)\n",
    "    print(f'text {i} result:', probs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10 (main, Jan 15 2022, 11:48:15) \n[Clang 12.0.0 (clang-1200.0.32.29)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
